package cn.stylefeng.guns.modular.ht.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.ht.entity.Report;
import cn.stylefeng.guns.modular.ht.mapper.ReportMapper;
import cn.stylefeng.guns.modular.ht.model.params.ReportParam;
import cn.stylefeng.guns.modular.ht.model.result.ReportResult;
import cn.stylefeng.guns.modular.ht.service.ReportService;
import cn.stylefeng.guns.sys.core.constant.factory.ConstantFactory;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 健康状况上报 服务实现类
 * </p>
 *
 * @author
 * @since 2020-02-01
 */
@Service
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements ReportService {

    @Override
    public void add(ReportParam param) {
        Report entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ReportParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(ReportParam param) {
        Report oldEntity = getOldEntity(param);
        Report newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ReportResult findBySpec(ReportParam param) {
        return null;
    }

    @Override
    public List<ReportResult> findListBySpec(ReportParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ReportParam param) {
        Page pageContext = getPageContext();
        if (ShiroKit.hasAnyRoles("company_admin")) {
            Long deptId = ShiroKit.getUser().getDeptId();
            List<Long> subDeptId = ConstantFactory.me().getSubDeptId(deptId);
            subDeptId.add(deptId);
            param.setDeptIds(subDeptId);
        }
        String reportTimeStr = DateUtil.format(param.getReportTime(), "yyyy-MM-dd");
        param.setReportTimeStr(reportTimeStr);
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);

    }

    public LayuiPageInfo findReporteds(ReportParam param) {
        Page pageContext = getPageContext();
        String today = DateUtil.today();
        param.setToday(today);
        IPage page = this.baseMapper.reportedList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<ReportResult> allReportedList(ReportParam param) {
        return this.baseMapper.allReportedList(param);
    }

    @Override
    public int countHealthy(ReportParam param) {
        return this.baseMapper.countHealthy(param);
    }

    @Override
    public int countAlert(ReportParam param) {
        return this.baseMapper.countAlert(param);
    }

    private Serializable getKey(ReportParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Report getOldEntity(ReportParam param) {
        return this.getById(getKey(param));
    }

    private Report getEntity(ReportParam param) {
        Report entity = new Report();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
